﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TemaAdministration.Models;

namespace TemaAdministration.Controllers
{ 
    public class UserController : Controller
    {
        private F11ITNET2Gr7Entities db = new F11ITNET2Gr7Entities();

        //
        // GET: /User/

        public ViewResult Index()
        {
            var gpsysusers = db.GPSysUsers.Include("Doctor");
            return View(gpsysusers.ToList());
        }

        //
        // GET: /User/Details/5

        public ViewResult Details(long id)
        {
            GPSysUser gpsysuser = db.GPSysUsers.Single(g => g.UserID == id);
            return View(gpsysuser);
        }

        //
        // GET: /User/Create

        public ActionResult Create()
        {
            ViewBag.DocId = new SelectList(db.Doctors, "DocId", "Name");
            return View();
        } 

        //
        // POST: /User/Create

        [HttpPost]
        public ActionResult Create(GPSysUser gpsysuser)
        {
            if (ModelState.IsValid)
            {
                db.GPSysUsers.AddObject(gpsysuser);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            ViewBag.DocId = new SelectList(db.Doctors, "DocId", "Name", gpsysuser.DocId);
            return View(gpsysuser);
        }
        
        //
        // GET: /User/Edit/5
 
        public ActionResult Edit(long id)
        {
            GPSysUser gpsysuser = db.GPSysUsers.Single(g => g.UserID == id);
            ViewBag.DocId = new SelectList(db.Doctors, "DocId", "Name", gpsysuser.DocId);
            return View(gpsysuser);
        }

        //
        // POST: /User/Edit/5

        [HttpPost]
        public ActionResult Edit(GPSysUser gpsysuser)
        {
            if (ModelState.IsValid)
            {
                db.GPSysUsers.Attach(gpsysuser);
                db.ObjectStateManager.ChangeObjectState(gpsysuser, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.DocId = new SelectList(db.Doctors, "DocId", "Name", gpsysuser.DocId);
            return View(gpsysuser);
        }

        //
        // GET: /User/Delete/5
 
        public ActionResult Delete(long id)
        {
            GPSysUser gpsysuser = db.GPSysUsers.Single(g => g.UserID == id);
            return View(gpsysuser);
        }

        //
        // POST: /User/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(long id)
        {            
            GPSysUser gpsysuser = db.GPSysUsers.Single(g => g.UserID == id);
            db.GPSysUsers.DeleteObject(gpsysuser);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}